Moving Average as Foreign Exchange Trading Strategy

This exercise performs a back-testing for a MA strategy. First, I find the optimal MA parameters (number of look-back days for the two moving averages) by maximising trading profits over the first part of the sample (in-sample backtesting). Then, I use these optimal parameters to trade in the second part of the sample (out-of-sample backtesting).

In-sample back-testing results can never be achieved in live trading. Out-of-sample can be achieved in theory, gross of trading costs and slippage.

  1. Importing market data from Yahoo Finance.
  1. Ploting the data can be done with various packages.
  1. Defining the MA Strategy as a function of MA parameters i, j representing the sizes of the rolling MA windows.

The two methods give the same optimization result on the entire series. However, the second method should be faster, because it gets rid of the rolling mean calculation with Pandas.

  1. Find the optimal i, j parameters in the [0:N] window by maximizing the risk-adjusted value of the investment.
  1. Find the optimal i, j parameters in the full series by maximizing the risk-adjusted value of the investment, for the pure in-sample strategy backtesting.

It appears the optimal parameters are found in a small island above water, which makes me pesimistic about the success of the out-of-sample backtesting.

  1. Calculating out-of-sample log-returns.
  1. Plotting the cumulative log-returns of the MA strategy in and out of sample and of holding long-only CAD versus USD.
  1. Plotting the two moving averages and the long-short regimes for the combined strategy.
  1. Calculating basic descriptive statistics for the MA strategy log-returns.

The graphs show only small differences between in-sample and out-of sample testing. The statistics below add more precision to the story.

  1. Plotting rolling performance of the combined (in-sample + out-of-sample) MA strategy.

As expected, the Out-of-Sample backtested performance does not beat In-sample backtesting. The performance is less bad than I expected, except for a long drawdown of about 17%, lasting aproximately five months.

This strategy could be improved by: